﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Deflection
{
    class Formation
    {
        public int CurrentNodeIndex { get; set; }

        /// <summary>
        /// The Nodes on the path to travel to
        /// </summary>
        public List<Vector2> Nodes { get; set; }

        /// <summary>
        /// The center of the path on the screen
        /// </summary>
        public Vector2 position = Vector2.Zero;

        /// <summary>
        /// The path for the formation to follow
        /// </summary>
        public Path Path { get; set; }

        /// <summary>
        /// The scale of the path, bigger = Nodes are more spread out
        /// </summary>
        public float scale = 1.0f;

        public Formation()
        {
            CurrentNodeIndex = 0;
            Nodes = new List<Vector2>();
        }

        public Formation(Vector2 position, List<Vector2> nodes)
        {
            this.position = position;
            
            Nodes = nodes;
            CurrentNodeIndex = 0;
        }


        public void Update(GameTime gameTime)
        {
            if (HasPath())
            {

            }
        }

        public bool HasPath()
        {
            if (Path == null)
            {
                return false;
            }
            return true;                     
        }
        /// <summary>
        /// Creates an enemy of specific type at each node of the formation
        /// </summary>
        /// <param name="enemyType"></param>
        public void CreateFormation(Enemy.EnemyType enemyType)
        {
            Enemy enemy;
            for (int i = 0; i < Nodes.Count; i++)
            {
                // Give the enemy its formation and its formation index (i); 
                enemy = EnemyFactory.Instance().CreateEnemy(EnemyFactory.ScreenSide.Right, enemyType);
                enemy.Position = position + (Nodes[i] * scale);
                enemy.formation = this;
                enemy.formationNodeIndex = i;
            }
        }

        /// <summary>
        /// Checks if the path has all the right components set, ready to be used
        /// </summary>
        /// <returns>True if valid</returns>
        public bool CheckFormationIsValid()
        {
            if(Nodes.Count < 1) return false;
            if(position == Vector2.Zero) return false;
            if(scale <= 0.0f) return false;

            // All is good
            return true;          
        }

        /// <summary>
        /// Gets the Vector2 position of the current formation node
        /// </summary>
        /// <returns>The Vector2 of the node</returns>
        public Vector2 GetCurrentPositionInFormation(int index)
        {
            return position + (Nodes[index] * scale);
        }

        /// <summary>
        /// Gets the next node index, will return 0 if the next index would be greater than the length of the path
        /// </summary>
        /// <param name="index">Current index</param>
        /// <returns>The index of the next node on the path</returns>
        public int Next(int index)
        {
            if (IsEndOfFormation(index))
            {
                return 0;
            }
            return (index + 1);
        }

        /// <summary>
        /// Decreases the Node index
        /// </summary>
        public void Previous()
        {
            CurrentNodeIndex--;
            if (CurrentNodeIndex < 0)
            {
                CurrentNodeIndex = Nodes.Count;
            }
        }

        /// <summary>
        /// Checks if its at the end of the path
        /// </summary>
        /// <returns>True if at the end of the path</returns>
        public bool IsEndOfFormation(int index)
        {
            return index >= Nodes.Count;
        }

    }
}
